from django.shortcuts import redirect


class UserLoginMiddleware(object):

    def process_request(self, request):
        # 获取请求路径 判断请求的路径是那个功能
        path = request.path
        if path in ['/user/user', '/user/order', '/user/address', '/order/place']:
            if not request.user.is_authenticated():
                HTTP_REFERER = request.META['HTTP_REFERER']
                if path == '/order/place':
                    # 订单页面不能直接进行跳转需要进入购物车页面
                    path = '/cart/show'
                return redirect('/user/login?next=' + path)
